package Screens;

import Controllers.HdChamadosJpaController;
import Controllers.HdUsersJpaController;
import Entities.HdChamados;
import Entities.HdUsers;
import TableModels.ChamadosUsersTableModel;
import TableModels.UsersRelatorioTableModel;
import Util.EMFConection;
import Util.UserLogado;
import java.util.List;

/**
 *
 * @author Ferrari
 */
public class TelaPrincipalUsuario extends javax.swing.JFrame {

    /**
     * Creates new form TelaPrincipalUsuario
     */
    public TelaPrincipalUsuario() {
        initComponents();
        btRefreshActionPerformed(null);
        tbChamados.getTableHeader().setReorderingAllowed(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbChamados = new javax.swing.JTable();
        btNovoChamado = new javax.swing.JButton();
        btVizualizar = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        btRefresh = new javax.swing.JButton();
        btEditarUser = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        tbChamados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbChamados);

        btNovoChamado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/new.png"))); // NOI18N
        btNovoChamado.setText("Novo Chamado");
        btNovoChamado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoChamadoActionPerformed(evt);
            }
        });

        btVizualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Relatório.png"))); // NOI18N
        btVizualizar.setText("Vizualizar");
        btVizualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVizualizarActionPerformed(evt);
            }
        });

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/exit.png"))); // NOI18N
        btSair.setText("Sair");
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });

        btRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Refresh.png"))); // NOI18N
        btRefresh.setText("Atualizar Chamados");
        btRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRefreshActionPerformed(evt);
            }
        });

        btEditarUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Editar.png"))); // NOI18N
        btEditarUser.setText("Editar Usuário");
        btEditarUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarUserActionPerformed(evt);
            }
        });

        jLabel1.setText("Chamados:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btNovoChamado, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btVizualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btEditarUser, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btSair, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btNovoChamado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btEditarUser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btVizualizar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-784)/2, (screenSize.height-449)/2, 784, 449);
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoChamadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoChamadoActionPerformed
        new CadastroChamado().setVisible(true);
    }//GEN-LAST:event_btNovoChamadoActionPerformed

    private void btRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRefreshActionPerformed
        HdChamadosJpaController chamadosJpaController = new HdChamadosJpaController(EMFConection.getInstance());
        
        List<HdChamados> list = chamadosJpaController.findHdChamadosByCoUser(UserLogado.getInstance());
        
        tbChamados.setModel(ChamadosUsersTableModel.addAllChamados(list));
    }//GEN-LAST:event_btRefreshActionPerformed

    private void btVizualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVizualizarActionPerformed
        HdChamadosJpaController chamadosJpaController = new HdChamadosJpaController(EMFConection.getInstance());
        
        HdChamados chamado = chamadosJpaController.findHdChamados(Integer.valueOf(tbChamados.getValueAt(tbChamados.getSelectedRow(), 0).toString()));
        if (chamado != null){
            new VizualizarChamado(chamado).setVisible(true);
        }
    }//GEN-LAST:event_btVizualizarActionPerformed

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btSairActionPerformed

    private void btEditarUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarUserActionPerformed
        new EditarUser(UserLogado.getInstance()).setVisible(true);
    }//GEN-LAST:event_btEditarUserActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEditarUser;
    private javax.swing.JButton btNovoChamado;
    private javax.swing.JButton btRefresh;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btVizualizar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbChamados;
    // End of variables declaration//GEN-END:variables
}
